package com.example.as_sum.imageutil;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.example.as_sum.R;
import com.example.as_sum.activity.RootActivity;
import com.example.as_sum.util.Log;
import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ImageActivity extends RootActivity {
    String [] imageList=new String[]{
            "http://p0.so.qhmsg.com/sdr/1228_768_/t011931f7577dd9d3af.jpg",
            "http://p1.so.qhmsg.com/sdr/1228_768_/t0101c70cea6f53b3b1.jpg",
            "http://p4.so.qhmsg.com/sdr/1228_768_/t01e3bfbce93ed10869.jpg",
            "http://p0.so.qhmsg.com/sdr/1228_768_/t011daac5e19b39985a.jpg",
            "http://p4.so.qhmsg.com/sdr/1228_768_/t0139714829f586ed19.jpg",
    };
    private static final String TAG = "----Fresco---->";
    @Bind(R.id.facebook_img)
    SimpleDraweeView facebookImg;
    @Bind(R.id.facebook_img2)
    SimpleDraweeView facebookImg2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        ButterKnife.bind(this);
        count=0;
    }
    private int count;
    @OnClick({R.id.facebook_btn, R.id.facebook_btn2, R.id.facebook_btn3, R.id.facebook_btn4, R.id.facebook_btn5, R.id.facebook_btn6})
    public void onClick(View view) {
       count=count%5;
        String url=imageList[count];
        switch (view.getId()) {
            case R.id.facebook_btn:
                if(FrescoUtils.isCached(url)){
                    Log.e(TAG,"已经缓存了");
                    File file=FrescoUtils.getFileFromDiskCache(url);
                    Uri uri=Uri.fromFile(file);
                    facebookImg.setImageURI(uri);
                }else{
                    Log.e(TAG,"没有缓存");
                    facebookImg.setImageURI(url);
                }
                break;
            case R.id.facebook_btn2:
                FrescoUtils.clearDiskCache();
                break;
            case R.id.facebook_btn3:
                FrescoUtils.clearCacheByUrl(imageList[count]);
                break;
            case R.id.facebook_btn4:
                if(FrescoUtils.isCached(url)){
                    Log.e(TAG,"已经缓存了");
                    File file=FrescoUtils.getFileFromDiskCache(url);
                    Uri uri=Uri.fromFile(file);
                    facebookImg2.setImageURI(uri);
                }else{
                    Log.e(TAG,"没有缓存");
                    facebookImg2.setImageURI(url);
                }
                break;
            case R.id.facebook_btn5:
                FrescoUtils.loadUrl(imageList[count],facebookImg2,null,100,80,null);
                break;
        }
        count++;
    }

    /**
     * 使用GenericDraweeHierarchy来设置占位图，失败图，设置动画，圆角等
     * @return
     */
    private GenericDraweeHierarchy sePro(){
        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setPlaceholderImage(R.drawable.bg)
                .setProgressBarImage(new ProgressBarDrawable())
               // .setBackgrounds(backgroundList)//背景
                //.setOverlays(overlaysList)//覆盖物
                .build();
        //圆角
        RoundingParams roundingParams = hierarchy.getRoundingParams();
        roundingParams.setCornersRadius(10);
        hierarchy.setRoundingParams(roundingParams);
        //圆
        RoundingParams roundingParam = RoundingParams.fromCornersRadius(5f);
        roundingParam.setBorder(R.color.colorGray, 1.0f);
        roundingParam.setRoundAsCircle(true);

        return  hierarchy;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
